package com.googlecode.garbagecan.perf4jstudy;

import org.perf4j.LoggingStopWatch;
import org.perf4j.StopWatch;

// java -cp .:./classes:./lib/perf4j-0.9.10.jar perf4j/Test1
// java -jar lib/perf4j-0.9.10.jar perf4j.log
// java -jar lib/perf4j-0.9.10.jar -f csv perf4j.log
// java -jar lib/perf4j-0.9.10.jar --graph perfGraphs.html perf4j.log
public class Test2 {
	public static void main(String[] args) throws Exception {
		test1();
		test2();
		test3();
	}

	public static void test1() throws Exception {
		StopWatch stopWatch = new LoggingStopWatch("test1");
		Thread.sleep((long) (Math.random() * 1000L));
		stopWatch.stop();
	}

	public static void test2() throws Exception {
		for (int i = 0; i < 3; i++) {
			StopWatch stopWatch = new LoggingStopWatch("test2");
			Thread.sleep(1000);
			stopWatch.stop();
		}
	}

	public static void test3() throws Exception {
		for (int i = 0; i < 5; i++) {
			StopWatch stopWatch = new LoggingStopWatch();
			try {
				long sleepTime = (long) (Math.random() * 1000L);
				Thread.sleep(sleepTime);
				if (sleepTime > 500L) {
					throw new Exception("Throwing exception");
				}
				stopWatch.stop("test3.success", "Sleep time was < 500 ms");
			} catch (Exception e) {
				stopWatch.stop("test3.failure", "Exception was: " + e);
			}
		}
	}
}
